package com.bwie.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.bwie.common.constants.Constants;
import com.bwie.common.domain.Delivery;
import com.bwie.common.domain.Orders;
import com.bwie.common.exceptions.BusinessException;
import com.bwie.system.mapper.OrdersMapper;
import com.bwie.system.service.OrdersService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class OrdersServiceImpl implements OrdersService {
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public PageInfo<Orders> ordersList(Orders orders) {
        PageHelper.startPage(orders.getPageNum(),orders.getPageSize());
        if(orders.getPageNum()==1&&orders.getStartTime()==null&&orders.getEndTime()==null&&
                StrUtil.isBlank(orders.getOrderCode())&&StrUtil.isBlank(orders.getBusinessName())&&
                StrUtil.isBlank(orders.getBuyersName())&&StrUtil.isBlank(orders.getDeliveryName())&&
                StrUtil.isBlank(orders.getGoodsName())){
            Boolean aBoolean = redisTemplate.hasKey(Constants.ORDERS_LIST);
            if(aBoolean){
                PageInfo<Orders>  pageInfo = (PageInfo<Orders>) redisTemplate.opsForValue().get(Constants.ORDERS_LIST);
                return pageInfo;
            }
               List<Orders> list = ordersMapper.ordersList(orders);
               PageInfo<Orders> pageInfo = new PageInfo<>(list);
               redisTemplate.opsForValue().set(Constants.ORDERS_LIST,pageInfo,50, TimeUnit.MINUTES);
               return pageInfo;
        }
        List<Orders> list = ordersMapper.ordersList(orders);
        PageInfo<Orders> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void delAll(List<Integer> ordersIds) {
        if (ordersIds==null){
            throw new BusinessException("参数异常");
        }
        ordersMapper.delAll(ordersIds);
        redisTemplate.delete(Constants.ORDERS_LIST);

    }

    @Override
    public Orders findOrder(Integer ordersId) {
        if(ordersId==null){
            throw new BusinessException("参数异常");
        }
        Orders orders = ordersMapper.findOrder(ordersId);
        return orders;
    }

    @Override
    public void updOrderState(Integer ordersId) {
        if(ordersId==null){
            throw new BusinessException("参数为空");
        }
        if(ordersId==1){
            ordersMapper.updOrderState(ordersId);
        }


    }
    @Override
    public Delivery findDelivery(Integer deliveryId) {
        if(deliveryId==null){
            throw new BusinessException("参数为空");
        }
        Delivery delivery = ordersMapper.findDelivery(deliveryId);
        return delivery;
    }
}
